$(function(){
	$('#hd-menu summary').click(function(e){
		var el = $(this).parent();
        if(el.hasClass('hd-active')){
        	el.removeClass('hd-active').attr('open', 0);
        }else{
        	el.addClass('hd-active').removeAttr('open');
        }
        e.stopPropagation();
    });

    $('#hd-menu').click(function(e){
    	e.stopPropagation();
    });

    //搜索
    $('#hd-search form').submit(function(){
        var key = $.trim($(this).find('input').val());
        if(key == ''){
            $(this).find('input').focus();
        }else{
            window.location.href = '/share/search/keyword/'+ key;
        }
        return false;
    });

    $('body').click(function(){
       $('#hd-menu').removeClass('hd-active').removeAttr('open');
    });

    if(window.location.href.indexOf('/u/sign/') == -1){
        localStorage.removeItem('mt-user-data');
    }

    $('.lazyload').lazyload();

    $('.d-unlike').each(function(){
        var el = $(this);
        var mid = $(this).data('mid');
        var sid = $(this).data('sid');
        var param = '';
        var tag = $('.ds-tag a').eq(0).html();
        var diff = 1;

        if(mid){param = 'image_id='+ mid +'&tag='+ tag;}
        if(sid){
            diff = $('#detail img').size();
            param = 'share_id='+ sid;
        }

        el.data('param', param).data('diff', diff);

        if(param != ''){
            $.getJSON('/share/ajax/g/like_state/?'+ param, function(result){
                el.data('likenum', result.total).find('i').html(result.total);
                if(result.user_like){
                    el.addClass('d-like').data('likeState', 'like');
                }else{
                    el.data('likeState', 'unlike');
                }
            }) 
        }

        if(!siteConfig.user){
            el.find('a').attr({'href': '/u/sign?next='+ window.location.href});
        }else{
            el.find('a').click(function(){
                var action = '';
                var likeNum = el.data('likenum');
                var diff = el.data('diff');
                var className = '';
                var newLikeState = '';

                if(el.data('likeState') == 'unlike'){
                    likeNum = likeNum + diff;
                    className = 'd-unlike d-like';
                    newLikeState = 'like';
                    el.find('span').html('取消喜欢');
                    action = '/share/ajax/g/addlike/?'+ el.data('param');
                }else{
                    likeNum = ((likeNum - diff) < 0) ? 0 : (likeNum - diff);
                    className = 'd-unlike';
                    newLikeState = 'unlike';
                    action = '/share/ajax/g/removelike/?'+ el.data('param');
                }

                $.getJSON(action, function(result){});
                el.attr('class', className).data({
                    'likenum': likeNum,
                    'likeState': newLikeState
                }).find('i').html(likeNum);
            });
        }
    });
});